<template>
  <div class="allsearch">
    <section class="search-content">
      <el-row :gutter="4" type="flex" justify="start">
        <el-col :span="24">
          <el-date-picker
            v-model="cycle"
            :picker-options="picker"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="|"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="width:300px"
            @change="handleFilter"
          />
          <el-input v-model="username" style="width:200px;" placeholder="用户名" @keyup.enter.native="handleFilter"/>
          <el-button type="primary" size="medium" @click="handleFilter">搜索</el-button>
          <el-button size="medium" type="info" @click="clearParams">清空</el-button>
        </el-col>
      </el-row>
    </section>
    <subtable
      :table-label="zeroLabel"
      :table-data="zeroData.slice((currentpage-1)*pagesize,currentpage*pagesize)"
    >
      <el-table-column slot="first" label="用户" min-width="20" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handleClickDialog(scope)">{{ scope.row.operatorName }}</el-button>
        </template>
      </el-table-column>
    </subtable>
    <pagination
      :total="total"
      :current-page="currentpage"
      :page-size="pagesize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <user-dialog :visiabledialog="userdialog" :dialog-data="dialogData" @changeVisiable="userdialog = false"/>
  </div>
</template>

<script>
import UserDialog from '@/components/Dialog'
import Subtable from '@/components/Eltable'
import Pagination from '@/components/Pagination' // 分页组件
import { getRewardPointAccountFlowList } from '@/api/operate/rewardPoint' // 数据接口
import { getUserDetail } from '@/api/operate/userDetail' // 数据接口
import { getTenantId } from '@/utils/auth'
export default {
  components: {
    Subtable,
    Pagination,
    UserDialog
  },
  data() {
    return {
      userdialog: false,
      dialogData: {},
      total: 0,
      currentpage: 1,
      pagesize: 10,
      zeroLabel: [
        {
          label: '操作',
          width: '10',
          prop: 'operation'
        },
        {
          label: '时间/ip',
          width: '10',
          prop: 'timeAndIp'
        }
      ],
      zeroData: [],
      listFilter: [],
      username: '',
      cycle: '',
      picker: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    handleClickDialog(scope) {
      getUserDetail(scope.row.operator).then(response => {
        this.dialogData = response.data.item
      })
      this.userdialog = true
    },
    handleSizeChange(val) {
      this.currentpage = 1
      this.pagesize = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleCurrentChange(val) {
      this.currentpage = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleFilter() {
      this.zeroData = this.listFilter.filter(item => {
        if (this.cycle && (new Date(item.createTime) < this.cycle[0] || new Date(item.createTime) > this.cycle[1])) return false
        if (this.username && item.operatorName.toLocaleLowerCase().indexOf(this.username.toLocaleLowerCase()) === -1) return false
        return true
      })
      this.total = this.zeroData.length
    },
    clearParams() {
      this.cycle = []
      this.username = ''
      this.getData()
    },
    getData() {
      getRewardPointAccountFlowList().then(response => {
        this.zeroData = response.data.items.filter(item => {
          if (item.tenantId !== getTenantId()) return false
          return true
        })
        this.zeroData.map(item => {
          if (item.type === 'inflow') {
            item.operation =
              '发放' +
              item.amount +
              '金币到' +
              item.name +
              '账户 备注：' +
              item.memo
          } else if (item.type === 'outflow') {
            item.operation =
              '从' +
              item.name +
              '账户扣减' +
              item.amount +
              '金币 备注：' +
              (item.memo === undefined ? '无' : item.memo)
          }
          if (item.createTime) item.createTimeStr = new Date(item.createTime).toLocaleString().replace(/[\u4e00-\u9fa5]/g, '')
          item.timeAndIp = (item.createTimeStr ? item.createTimeStr : '') + '\n' + (item.ipAddress ? item.ipAddress : '')
        })
        this.total = this.zeroData.length
        this.listFilter = this.zeroData
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.allsearch {
  margin-top: 0px;
  padding: 0;
  .search-content {
    width: 100%;
  }
}
</style>
